import { getUserInfoUrl, isDh } from "@/command/apiTool";
import { useDetail, useList } from "@/hooks";
import { getAction, postAction } from "@/command/netTool";
import { reactive, ref, watch, onMounted } from "vue";
import { useRoute } from "vue-router";
import router from "@/router";
import * as vant from "vant";
const { showFailToast, showSuccessToast } = vant;

export const useRequest = () => {
  const form = reactive({
    pageNo: 1,
  });
  const checked = ref(false);
  const route = useRoute();

  const page = ref();
  // const statsInfo = ref({
  //   status: "",
  // });

  watch(
    () => checked.value,
    (e) => {
      // console.log("eee,", e);
      !e && confirm({ stats: 0 });
      // e ? confirm({ stats: 1 }) : confirm({ stats: 0 });
    }
  );
  // onMounted(() => {
  //   !isDh() &&
  //     getAction("/api/H5/service/helpWork/small/getInfoStats").then((e) => {
  //       checked.value = e.status == "0" ? false : true;
  //       statsInfo.value = e.status;
  //     });
  // });

  const [statsInfo, statsInfoControl] = useDetail(
    getUserInfoUrl({
      dh: "",
      xh: "/api/H5/service/helpWork/small/getInfoStats",
    }),
    {
      status: "",
    },
    {
      callBack: (data: any) => {
        checked.value = data.status == "0" ? false : true;
        return data;
      },
    }
  );

  const confirm = (params: any) => {
    getAction(
      "/api/H5/service/helpWork/small/updateInfoStats",
      { ...params },
      { isResolve: true }
    ).then((e) => {
      showSuccessToast(e.data || e.msg || e);
      statsInfoControl.refresh();
      // showSuccessToast(e.msg)
    });
  };

  //申请列表--大户
  //最新资讯--小户

  const [newInfoList] = useDetail(
    getUserInfoUrl({
      dh: "",
      xh: "/api/H5/service/helpWork/small/newInfo",
    }),
    {
      phone: "",
      beforeTime: "",
    }
  );

  const [applyNews] = useDetail(
    getUserInfoUrl({
      dh: "/api/H5/service/helpWork/heavy/applyNews",
      xh: "",
    }),
    {
      beforeTime: "",
      id: "",
      info: "",
      phone: "",
      typeName: "",
    }
  );

  const routerClick = (e: any) => {
    router.push({ path: e });
  };

  // 申请用工
  const applyWork = (id: any) => {
    getAction(`/api/H5/service/helpWork/small/applyWork?workId=${id}`).then(
      (e) => {
        showSuccessToast("申请成功");
        setTimeout(() => {
          page.value.reset();
        }, 1000);
      }
    );
  };

  return {
    form,
    page,
    checked,
    newInfoList,
    applyNews,
    routerClick,
    applyWork,
    statsInfo,
  };
};
